{extend name="admin/admin" /}

{block name="menu"}

<!--查看权限模态框-->

<div id="roleall" style="margin-top:150px;" class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog"
     aria-labelledby="myLargeModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <h1 style="text-align: center; font-size:20px;color:white; "></h1>


        <div id="box" style="color:white;font-size: 20px;" class="form-group">


        </div>


    </div>
</div>
<!--end模态框-->
<!--add模态框-->

<div style="margin-top:200px;" id='add' class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog"
     aria-labelledby="myLargeModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <h1 style="text-align: center; font-size:20px;color:white; ">添加角色</h1>

        <div class="form-group">
            <label for="inputEmail" class="col-sm-2 control-label"> </label>
            <div class="col-sm-10">
                <input type="email"
                       class="form-control" onkeyup="value=value.replace(/[^\w\u4E00-\u9FA5]/g, '')" required
                       name="role" id="inputEmail" placeholder="角色名 ">
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-2 control-label">Email</label>
            <div style="margin-top:30px;" class="col-sm-10">
                <input type="email"
                       class="form-control" required onkeyup="value=value.replace(/[^\w\u4E00-\u9FA5]/g, '')"
                       name="description" id="inputEmail3" placeholder="描述">
            </div>
        </div>
        <div style="margin-left:120px;color:white;font-size: 20px;" id="box2" class="form-group">
            分配权限
            {volist name="list" key="k" id="v" }

            <input type="checkbox" name="permission" value="{$v['id']}">{$v['name']}

            {/volist}

        </div>
        <button type="button" style="margin-top: 30px;margin-left: 400px;"
                onclick="doadd()" class="btn btn-success">提交
        </button>
    </div>
</div>
<!--end模态框-->


<h1 style="text-align: center; font-size:50px;">角色配置<span style="font-size:15px;color:red;">创建角色&给角色分配权限</span>
</h1>

<div class="table-responsive">
    <table class="table" id="table2">
        <tr>
            <th width="5%" class="active">编号</th>
            <th width="25%" class="success">角色名</th>
            <th width="25%" class="warning">描述</th>
            <th width="25%" class="warning">权限</th>
            <th width="25%" class="info">状态</th>
            <th width="10%" class="danger">
                <button data-toggle="modal" data-target="#add" class="btn btn-info">
                    添加
                </button>
            </th>
        </tr>
        {volist name="da" key="k" id="v" }
        <tr>
            <td class="active">{$v['id']}</td>
            <td class="success">{$v['name']}</td>
            <td class="warning">{$v['description']}</td>
            <td class="warning">


                <button onclick="permissionall(this)" data-toggle="modal" data-target="#roleall" class="btn btn-info">
                    查看权限
                </button>
            </td>
            <td class="info">
                {if condition="$v['status'] eq 1"}
                启用
                {else}
                禁用
                {/if}
            </td>
            <td class="danger">

                <button onclick="dodel(this)" class="btn btn-primary">删除</button>
            </td>
        </tr>
        {/volist}
    </table>

    {$da->render()}
</div>

<script>

    //查看 权限
    function permissionall(s) {
        var admin_id = $(s).parents('tr').find("td:eq(0)").text();
        $.ajax({
            method: 'post',
            url: "{:url('admin/Position/permissionall')}",
            data: {
                "id": admin_id,
            },
            dataType: 'json',
            success: function (data) {
                $('#box').html('');
                for (var i = 0; i < data.length; i++) {
                    $('#box').append(data[i]['name'] + ' ');
                }

            },
            error: function () {
            }
        });
    }


    //添加角色 和 权限
    function doadd() {
        var description = $("input[ name='description']").val();
        var role = $("input[ name='role']").val();

        var  permissions=[];
        $('input[name="permission"]:checked').each(function () {
            permissions.push($(this).val());
        });

        $.ajax({
            method: 'post',
            url: "{:url('admin/Position/addrole')}",
            data: {
                'description': description,
                'role': role,
                'permission': permissions,
            },
            dataType: 'json',
            success: function (data) {
                if (data == 1) {
                    alert('添加成功');
                    location.reload();
                } else {
                    alert('输入有误,请重试');
                    // location.reload();
                }

            },
            error: function () {
                alert('输入有误,请重试！！');
                // alert('服务器繁忙,请重试');
                // location.reload();

            }
        });
    }


    //删除角色
    function dodel(b) {

        var id = $(b).parents('tr').find("td:eq(0)").text();

        $.ajax({
            method: 'post',
            url: "{:url('admin/Position/delrole')}",
            data: {
                'id': id,
            },
            dataType: 'json',
            success: function (data) {
                if (data == 1) {
                    alert('删除成功');
                    location.reload();
                    location.href = "http://www.tp5.com/position.html";
                } else {
                    alert('删除失败,请重试！');
                    location.reload();
                }

            },
            error: function () {
                alert('删除失败,请重试');
                // alert('服务器繁忙,请重试');
                location.reload();

            }
        });

    }
</script>
{/block}
